package com.web.js;

import java.util.*;

import com.database.daos.IProjectDao;
import com.database.objects.*;
import com.metrics.extractors.*;
import com.metrics.objects.*;
import com.metrics.statistics.StatisticsCalculator;

public class StatisticsJS {

	private static final String SEPARATOR = "%%";

	private IProjectDao projectDao;

	private StatisticsCalculator statisticsCalculator;

	private MetricExtractorMap metricExtractorMap;

	public String[] getStatistics(String name, String metricString, String typeString, String statusString, String start, String finish) {
		Project project = projectDao.getByName(name);
		Metric metric = Metric.valueOf(metricString);
		Type type = Type.valueOf(typeString);
		Status status = Status.valueOf(statusString);

		MetricExtractor extractor = metricExtractorMap.get(metric);

		Map<Statistic, Double> statisticsMap = statisticsCalculator.calculate(project, extractor, type, status, start, finish);
		String[] statistics = new String[statisticsMap.size()];
		int i = 0;
		for (Statistic statistic : statisticsMap.keySet()) {
			statistics[i] = statistic.getName() + SEPARATOR + statisticsMap.get(statistic);
			i++;
		}
		return statistics;
	}

	public void setProjectDao(IProjectDao projectDao) {
		this.projectDao = projectDao;
	}

	public void setStatisticsCalculator(StatisticsCalculator statisticsCalculator) {
		this.statisticsCalculator = statisticsCalculator;
	}

	public void setMetricExtractorMap(MetricExtractorMap metricExtractorMap) {
		this.metricExtractorMap = metricExtractorMap;
	}

}